﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using JustinAngel.MetroBlog.Common.IoC;
using JustinAngel.MetroBlog.Services.BlogPostService;
using JustinAngel.MetroBlog.Services.CategoryService;
using Microsoft.Practices.Unity;

namespace JustinAngel.MetroBlog.Controllers
{
    public class CategoryConstraint : IRouteConstraint
    {
        public CategoryConstraint()
        {
            IoC.BuildUp(this);
        }

        [Dependency]
        public ICategoryService CategoryService { get; set; }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (!values.ContainsKey(parameterName))
                return false;

            var link = (string)values[parameterName];

            return CategoryService.Exists(link);
        }
    }
}
